// 应用市场对外展示页面
package handler.store

import org.paic.vertx.httpwrapper.Result
import org.paic.d.D

final Map cates = [life: '生活服务']

final List flinks = [
	[name: 'Ping++', link: 'https://pingplusplus.com/'],
	[name: '百度LBS开放平台', link: 'http://developer.baidu.com/map/index.php'],
	[name: '图灵机器人', link: 'http://www.tuling123.com/openapi/'],
	[name: '蒲公英', link: 'http://www.pgyer.com/'],
	[name: 'DAOCloud', link: 'https://www.daocloud.io/'],
	[name: '聚合数据', link: 'http://www.juhe.cn/']
]

long expiredIndex = 1000 * 30
long lastVisitIndex = 0
Map dataIndex = null

// cache index
handler.filter('cacheStoreIndexAfter', handler.NEXT, '/store/index', true){req, resp ->
	log.info('no cache -- index')

	Result r = req.getLocal(handler.RESULT)
	dataIndex = r.viewData
	lastVisitIndex = System.currentTimeMillis()

	false
}

handler.filter('cacheStoreIndexPre', handler.PRE, '/store/index'){req, resp ->
	if(dataIndex != null && System.currentTimeMillis() < lastVisitIndex + expiredIndex){
		log.info('use cache -- index')
		resp.render('/store/index.html', dataIndex)
		return true
	}

	false
}


// *** *** *** *** *** *** *** *** *** *** *** ***
handler.get('/wx/store/index'){req, resp ->
	def data = [isIndex: true]
	data.user = req.session('_user')

	def d = D.gen()

	// 分类统计
	def ll = d.query('''
		select count(id) as num, cate from t_app group by cate order by count(id) 
	'''
	)
	for(one in ll){
		one.cateName = cates[one.cate]
	}
	data.apps = ll

	// 热门
	data.hots = d.query('select id,name from t_app where hot = 1 order by num_buy desc limit 8')

	// 最新
	data.recents = d.query('select id,name from t_app order by dd_modify desc limit 4')

	// 本周热门
	def dd = new Date()
	// 周一
	def begin = dd - dd.day
	def end = begin + 7

	data.hotsWeek = d.query('''
		select aa.num_visit, bb.id, bb.name from (
		select app_id, count(id) as num_visit from t_app_visit_log 
			where dd > ? and dd < ? group by app_id order by count(id) limit 5

		) aa left join t_app bb on aa.app_id = bb.id
	'''
	, [begin, end])

	// 友情链接
	data.flinks = flinks

	[view: '/store/index.html', viewData: data] as Result
}

// *** *** *** *** *** *** *** *** *** *** *** ***
// 查询列表
handler.getRegEx(/\/wx\/store\/search\/([^\/]+)/){req, resp ->
	String q = req.params.param0

	resp.end q
}

// *** *** *** *** *** *** *** *** *** *** *** ***
// 分类列表
handler.getRegEx(/\/wx\/store\/apps\/(\d+)/){req, resp ->
	String code = req.params.param0
	log.info code

	def data = [isApps: true]

	def list = []
	list << [id: 1, name: '环信即时通讯云', img: '1426838542922-7530', 
		num_comment: 111, num_buy: 123, num_visit: 400, fee: 100]
	list << [id: 2, name: '环信即时通讯云', img: '1426838542922-7530', 
		num_comment: 111, num_buy: 123, num_visit: 400, fee: 100]
	list << [id: 3, name: '环信即时通讯云', img: '1426838542922-7530', 
		num_comment: 111, num_buy: 123, num_visit: 400, fee: 100]
	list << [id: 4, name: '环信即时通讯云', img: '1426838542922-7530', 
		num_comment: 111, num_buy: 123, num_visit: 400, fee: 100]

	data.list = list

	resp.render('/store/apps.html', data)
}

// *** *** *** *** *** *** *** *** *** *** *** ***
// 详细
handler.getRegEx(/\/wx\/store\/app\/(\d+)/){req, resp ->
	String id = req.params.param0
	log.info id

	def data = [isApps: true]

	resp.render('/store/app-detail.html', data)
}

// *** *** *** *** *** *** *** *** *** *** *** ***
// 新闻消息
handler.get('/wx/store/news'){req, resp ->
	resp.render('/store/news.html', [isNews: true])
}

// *** *** *** *** *** *** *** *** *** *** *** ***
// 帮助
handler.get('/wx/store/help'){req, resp ->
	resp.render('/store/help.html', [isHelp: true])
}